﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;

namespace SimpleWebApp.Demo02
{
    public class StartupV2
    {
        private readonly IConfiguration _configuration;

        public StartupV2(IConfiguration configuretion)
        {
            _configuration = configuretion;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<FoobarOptions>(_configuration);
        }

        public void Configure(IApplicationBuilder app, IOptions<FoobarOptions> optionAsscessor)
        {
            var options = optionAsscessor.Value;
            var json = JsonSerializer.Serialize(options);
            app.Run(async context =>
           {
               context.Response.ContentType = "text/html";
               await context.Response.WriteAsync($"<pre>{json}</pre>");
           });
        }
    }
}
